package com.woniuxy.woniuhis.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.deepoove.poi.XWPFTemplate;
import com.woniuxy.woniuhis.entity.SurgeryConfirmForm;

@Controller
@RequestMapping("/surgeryconfirm")
public class SurgeryComfirmController {
	
    /**
     * 	下载保存时中文文件名的字符编码转换方法
     */
    public String toUTF8String(String str) {
        StringBuffer sb = new StringBuffer();
        int len = str.length();
        for (int i = 0; i < len; i++) {
            // 取出字符中的每个字符
            char c = str.charAt(i);
            // Unicode码值为0~255时，不做处理
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else { // 转换 UTF-8 编码
                byte b[];
                try {
                    b = Character.toString(c).getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    b = null;
                }
                // 转换为%HH的字符串形式
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0) {
                        k &= 255;
                    }
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }
	/**
	 * 	生成手术同意书word文件，并且立即下载
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/generate")
	public String genernateSurgeryConfirm(SurgeryConfirmForm form,HttpServletRequest req,HttpServletResponse resp) {
		try {
			// 数据验证，SurgeryConfirmForm
			System.out.println(form);
			// 读取模板
			ClassPathResource classPathResource = new ClassPathResource("doctemplate/template.docx");
			InputStream inputStream =classPathResource.getInputStream();
			// 编译模板生成word数据
			XWPFTemplate template = XWPFTemplate.compile(inputStream).render(form);
			// word存放位置
			String wordPath = "D:\\woniuxyJava\\Code_sts\\WoniuHIS\\src\\main\\resources\\doctemplate";
			// 生成word文件名
			String fileName = form.getInpatientName()+".docx";
			// 创建生成的word文件的文件对象
			File file = new File(wordPath,fileName);
			// 创建流写出word到本地
			FileOutputStream out = new FileOutputStream(file);
			template.write(out);
			
			// 下载刚才填写的生成的word
			// 设置响应头
			resp.setHeader("Content-Type", "application/x-msdownload");
            resp.setHeader("Content-Disposition", "attachment; filename="+ toUTF8String(fileName));
            // Servlet输出流
            ServletOutputStream servletOutStream = resp.getOutputStream();
            template.write(servletOutStream);
            // 关流
 			out.flush();
 			out.close();
 			template.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	// 上传手术同意书
	public String uploadSurgeryConfirmController() {
		return null;
	}
	
}
